package com.kim.rssreader.adapter;

/**
 * Title: 拖拽列表Adapter
 * @version 1.0
 */
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.kim.rssreader.domain.AbstractDomain;
import com.kim.rssreader.view.DragMessage;

public abstract class DragAdapter extends BaseAdapter {
	protected Context mContext;
	public List<AbstractDomain> mlist;
	public HashMap<Integer, Integer> subscribedColumnsIndex = new HashMap<Integer, Integer>();

	public DragAdapter(Context mContext, List<AbstractDomain> subscribedColumns) {
		this.mContext = mContext;
		this.mlist = subscribedColumns;
		fillData();
	}

	public void fillData() {
		subscribedColumnsIndex.clear();
		for (AbstractDomain abstractDomain : this.mlist) {
			subscribedColumnsIndex.put(abstractDomain.get_id(),
					DragMessage.STATIC_FLAG);
		}
	}

	public int getCount() {
		// 需要预留一个添加栏目的item
		// 不再需要预留一个添加栏目的item
		if (this.mlist != null) {
			return this.mlist.size();
		}
		return 0;
	}

	public Object getItem(int position) {
		return this.mlist.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public void addMsg(AbstractDomain abstractDomain) {
		this.mlist.add(abstractDomain);
	}

	public final void reFlag() {
		for (Entry<Integer, Integer> entry : subscribedColumnsIndex.entrySet()) {
			subscribedColumnsIndex.put(entry.getKey(), 0);
		}
		notifyDataSetChanged();
	}

	public final void swap(int srcPosition, int dragPosition) {
		Collections.swap(this.mlist, srcPosition, dragPosition);
		notifyDataSetChanged();
	}

	public final void setFlag(int position, int flag) {
		if (position > mlist.size() || position < 0) {
			return;
		}
		AbstractDomain abstractDomain = (AbstractDomain) mlist.get(position);
		subscribedColumnsIndex.put(abstractDomain.get_id(), flag);
		notifyDataSetChanged();
	}

	public abstract View getView(int paramInt, View paramView,
			ViewGroup paramViewGroup);
}
